//
//	ファイル名がＧＥＮＩＥのパーツデータっぽいかどうか調べる
//
function CheckFileNameforGenie( file )
{
	var	filename, ele[3], i;
	var	filelist[24], temp1, temp2, temp3;
	filelist = { "F1", "F2", "S1", "S2", "W1", "W2", "SD", "EG", "CP", "BR", "G1", "G2", "TK", "SL", "JT", "BL", "BG", "OT", "P1", "P2", "EF", "C1", "C2", "C3" };
	
	filename = toupper( fullpath( file ));
	Bunkai( filename, ele );
	if( !ele[1] != 4 )
		return FALSE;
	temp1 = substr( ele[1], 2 );
	temp2 = substr( ele[1], -2 );
	temp3 = strtoasc( temp2 )- strtoasc( "0" );
	if( ( temp3 >= 0 ) & ( temp3 <= 9 ))
	{
		temp2 = substr( temp2, -1 );
		temp3 = strtoasc( temp2 ) - strtoasc( "0" );
		if(( temp3 >= 0 ) & ( temp3 <= 9 ))
		{
			temp2 = atoi( substr( ele[1], -2 ));
			if( (temp2 < 0 ) | ( temp2 > 16 ))
				return FALSE;
		}
		else
			return FALSE;
	}
	else
		return FALSE;
	for( i = 0; i< 24; i++ )
	{
		if( temp1 == filelist[i] )
			return TRUE;
	}
	if( substr( temp1, 1 ) != "U" )
		return FALSE;
	temp2 = strtoasc(substr( temp1, -1 )) - strtoasc( "0" );
	if(( temp2 >= 0 ) & ( temp2 <= 9 ))
		return TRUE;
	else
		return FALSE;
}

//
//	ＧＥＮＩＥでつくったメカデータが読めるかもしれない
//
function LoadMecha()
{
	var	filename, i, j, k, temp, temp2, temp3, readfile, flag = OFF;
	var	key, sel1, sel2, temp_sel, old_max, deleteflag[MAXOBJECT];
	var	msg[5], ele[3], shikichou, uv[2][2];
	var	bodym[5], bodyd[5];

	filename = GetFile( "データロード", "", "fsc" );
	if( typeof( filename ) == BOOLEAN )
		return;
	if( access( filename ) == FALSE )
	{
		Warning();
		Message( "ファイルが存在しません" );
		return FALSE;
	}

	DlogOpen( "色調指定", 1 );
	msg = {"白", "青","赤","緑","紫" };
	DlogSelect( 0, "色調", msg, 0 );
	key = DlogWait();
	if( key == 0 )
		return FALSE;
	else
		shikichou = DlogAnswer( 0 );

	readfile = fullpath( filename );
	temp3 = MakeFile(substr( readfile, (!readfile -4 )), ".suf" );
	for( i = 0; i< Objects(); i++ )
	{
		temp2 = fullpath( ObjFile( i ));
		if( toupper(temp2) == toupper(temp3) )
		{
			flag = TRUE;
			break;
		}
	}
	if( flag == TRUE )
	{
		flag = FALSE;
		for( i = 0; i< Objects(); i++ )
		{
			if( toupper(temp3) == toupper( fullpath(ObjFile(i))))
			{
				if( ObjEdit(i) == TRUE )
				{
					flag = TRUE;
					break;
				}
			}
		}
		if( flag == TRUE )
		{
			Warning();
			DlogOpen( "", 2 , "いいえ", "はい" );
			DlogMessage( 0, "ファイルはすでに読み込まれています。" );
			DlogMessage( 1, "編集中のデータを破棄しますか？" );
			key = DlogWait();
			if( key == 0 )
				return FALSE;
		}
		SelectAll( TRUE );
		sel2 = Select();
		PolyInvisible( FALSE );
		i = 0;
		while( i < Objects() )
		{
			if( toupper(temp3) == toupper( fullpath( ObjFile(i))))
			{
				SelectObj( i, TRUE, SELECT_UPDATE );
				temp_sel = Select();
				Select( sel2 );
				PolyDelete( temp_sel );
				sel2 = Select();
				ObjDelete( i );
			}
			else
				i++;
		}
		Select( sel2 );
		PolyInvisible( invisible_flag );
	}
	FrmReadFile( readfile, OBJ_CREATE );
	sel1 = Select();
	bodym = { "BodyM","BodyM_BL","BodyM_RD","BodyM_GR","BodyM_MG" };
	bodyd = { "BodyD","BodyD_BL","BodyD_RD","BodyD_GR","BodyD_MG" };
	temp = AttrSearch( "BodyM" );
	temp2 = AttrSearch( "BodyD" );
	if( shikichou == 0 )
	{
	}
	else
	{
		temp3 = AttrSearch( bodym[shikichou] );
		if( temp3 < 0 )
			AttrName( temp , bodym[shikichou] );
		else
		{
			SelectAttr( temp, TRUE, SELECT_UPDATE );
			AttrChange( temp3 );
		}
		temp3 = AttrSearch( bodyd[shikichou] );
		if( temp3 < 0 )
			AttrName( temp2, bodyd[shikichou] );
		else
		{
			SelectAttr( temp2, TRUE, SELECT_UPDATE );
			AttrChange( temp3 );
		}
		Select( sel1 );
	}
	Bunkai( readfile, ele );
	temp = ele[0]+"..\\..\\atr\\genie.atr";
	temp2 = ele[0]+"..\\..\\..\\atr\\genie.atr";
	if( access( temp ))
		AttrReadFile( temp );
	else if( access( temp2 ))
		AttrReadFile( temp2 );
	else
	{
		Message(	 "GENIE.ATR が見つかりません。\n"
				+"使用するGENIE.ATRを指定してください。\n" );
		temp = GetFile( "データロード", "", "atr" );
		if( typeof( temp ) == BOOLEAN )
		{
			Warning();
			return;
		}
		if( access( temp ) == FALSE )
		{
			Warning();
			Message( "ファイルが存在しません" );
			return FALSE;
		}
		AttrReadFile( temp );
	}
	for( i = 0; i< Attrs(); i++ )
	{
		if( AttrMap( i ) == FALSE )
		{
			AttrMap( i, TRUE );
			AttrMapFile( i, MAPPINGFILE );
			uv[0] = {0,0};
			uv[1] = {MAPWIND-1, MAPWIND-1};
			SetAttrMapWind( i, uv[0], uv[1] );
			uv[0] = {0,0};
			uv[1] = {UVMAX-1, UVMAX-1};
			SetAttrMapSize( i, uv[0], uv[1] );
		}
	}
	temp = Objects()-1;
	ObjCurrent( temp );
	ObjEdit( temp, TRUE );
	UpdateObject();
	UpdateAttribute();
	Update( CLEAR );
}

//
//	新規作成forGENIE
//
function NewDataforGENIE()
{
	var	i, uv[2][2];
	var	flag = OFF;

	for( i = 0; i< Objects();i++ )
	{
		if( ObjEdit( i ))
		{
			flag = TRUE;
			break;
		}
	}
	if( flag )
	{
		Warning();
		DlogOpen( "", 1, "ｷｬﾝｾﾙ " ,"いいえ"," はい " );
		DlogMessage( 0, "データが変更されています。保存しますか？" );
		i = DlogWait();
		if( i == 0 )
			return;
		else if(( i == 2 ) | ( i == RET ))
			SaveAll();
	}
	PolyInvisible( FALSE );
	SelectAll( TRUE, SELECT_UPDATE );
	if( SelectPolygons != 0 )
		PolyDelete();
	while( Objects() > 0 )
		ObjDelete( 0 );
	while( Attrs() > 0 )
		AttrDelete( 0 );
	ObjAppend( default_objname, default_filename );
	AttrReadFile( model_dir+GENIEATRNAME );
	if( EXPERT != TRUE )
	{
		for( i = 0; i< Attrs(); i++ )
		{
			AttrMap( i, TRUE );
			AttrMapFile( i, MAPPINGFILE );
			uv[0] = {0,0};
			uv[1] = {MAPWIND-1, MAPWIND-1};
			SetAttrMapWind( i, uv[0], uv[1] );
			uv[0] = {0,0};
			uv[1] = {UVMAX-1, UVMAX-1};
			SetAttrMapSize( i, uv[0], uv[1] );
		}
	}
	UpdateObject();
	UpdateAttribute();
	AllInit();
	Polygon();
}


//
//	ＧＥＮＩＥにパーツを登録できるかもしれない
//
function InstallParts()
{
	var	const	MAXGENIEATR = 11;
	var	GenieAtrList[MAXGENIEATR];
	GenieAtrList = {	"BodyM","BodyD","White","Glass","GrayG","Brown",
				"WhiLg","BluLg","RedLg","YelLg","Black"	};

	var	obj, i, j, k, double_flag, filecheck_flag;
	var	sel1, sel2, temp, temp2, flag, ele[3];
	var	msg[2], atrlist[MAXATTR];

	msg = { " する ", "しない" };
	DlogOpen( "パーツ登録", 3 );
	DlogMessage( 0, "カレントオブジェクトをＧＥＮＩＥのパーツとして登録します" );
	DlogSelect( 2, "面二重化",msg, 0 );
	if( DlogWait() )
		double_flag = ( DlogAnswer(2) == 0 );
	else
		return FALSE;

	k = ObjtoAtr( ObjCurrent(), atrlist );
	for( i = 0; i< k; i++ )
	{
		temp = AttrName( atrlist[i] );
		flag = FALSE;
		for( j = 0; j< MAXGENIEATR; j++ )
		{
			if( temp == GenieAtrList[j] )
				flag = TRUE;
		}
		if( flag == FALSE )
		{
			Warning();
			Message( "アトリビュート "+ temp +"    \n"+
				 "はＧＥＮＩＥのアトリビュートとして使用できません" );
			return;
		}
	}

	if( CheckFileNameforGenie( fullpath( ObjFile( ObjCurrent()))))
	{
		DlogOpen( "", 3, "中止", "更新登録", "新規登録" );
		DlogMessage( 0, "このデータは既にパーツデータとして" );
		DlogMessage( 1, "登録されているようです。登録されて" );
		DlogMessage( 2, "いるデータを更新しますか？" );
		temp = DlogWait();
		if( temp == 1 )
			filecheck_flag = TRUE;
		else if( temp == 2 )
			filecheck_flag = FALSE;
		else
		{
			Warning();
			return FALSE;
		}
	}
	else
		filecheck_flag = FALSE;

	temp2 = Attrs();
	AttrReadFile( model_dir+GENIEATRNAME, ATTR_CREATE );
	for( i = 0; i< k; i++ )
	{
		for( j = temp2; j< Attrs(); j++ )
		{
			if( AttrName( j ) == AttrName(atrlist[i]) )
			{
				flag = FALSE;
				if( AttrCol( j ) != AttrCol( atrlist[i] ))
					flag = TRUE;
				if( AttrAmb( j ) != AttrAmb( atrlist[i] ))
					flag = TRUE;
				if( AttrDif( j ) != AttrDif( atrlist[i] ))
					flag = TRUE;
				if( AttrTra( j ) != AttrTra( atrlist[i] ))
					flag = TRUE;
				if( AttrSpc( j ) != AttrSpc( atrlist[i] ))
					flag = TRUE;
				if( AttrSize( j ) != AttrSize( atrlist[i] ))
					flag = TRUE;
				if( AttrRef( j ) != AttrRef( atrlist[i] ))
					flag = TRUE;
				if( AttrRfr( j ) != AttrRfr( atrlist[i] ))
					flag = TRUE;
				if( AttrAtt( j ) != AttrAtt( atrlist[i] ))
					flag = TRUE;
				if( flag == TRUE )
				{
					Warning();
					DlogOpen( "", 3, "中止" ,"実行" );
					DlogMessage( 0, "アトリビュート" + AttrName( atrlist[i] ) + "     \n" );
					DlogMessage( 1, "は編集されているため、ＧＥＮＩＥのアトリビュート  \n" );
					DlogMessage( 2, "として使用できません。データを元に戻しますか" );
					temp = DlogWait();
					if( temp )
					{
						AttrCol( atrlist[i], AttrCol( j ));
						AttrAmb( atrlist[i], AttrAmb( j ));
						AttrDif( atrlist[i], AttrDif( j ));
						AttrTra( atrlist[i], AttrTra( j ));
						AttrSpc( atrlist[i], AttrSpc( j ));
						AttrSize( atrlist[i], AttrSize( j ));
						AttrRef( atrlist[i], AttrRef( j ));
						AttrRfr( atrlist[i], AttrRfr( j ));
						AttrAtt( atrlist[i], AttrAtt( j ));
						AttrCode( atrlist[i], AttrCode( j ));
					}
					else
					{
						while( Attrs() > temp2 )
							AttrDelete( temp2 );
						return;
					}
				}
			}
		}
	}
	while( Attrs() > temp2 )
		AttrDelete( temp2 );

	if( !access( model_dir+"genie1.ini" ))
	{
		Message(	 "GENIE1 がインストールされているディレクトリが\n"
				+"見つかりません GENIE1 のインストールされている\n"
				+"ディレクトリにある GENIE1.INI を指定してください。\n" );
		temp = GetFile( "データロード", "", "ini" );
		if( typeof( temp ) == BOOLEAN )
		{
			Warning();
			return;
		}
		if( access( temp ) == FALSE )
		{
			Warning();
			Message( "ファイルが存在しません" );
			return FALSE;
		}
		Bunkai( temp, ele );
		if( toupper(ele[1]+"."+ele[2]) != "GENIE1.INI" )
		{
			Warning();
			Message( "指定されたファイルは GENIE1.INI ではありません。" );
			return;
		}
	}
	else
		ele[0] = model_dir;

	sel1 = Select();
	SelectAll( TRUE );
	sel2 = Select();
	PolyInvisible( FALSE );
	SelectObj( ObjCurrent(), TRUE, SELECT_UPDATE );
	if( SelectPolygons() == 0 )
	{
		Warning();
		if( novice_flag == TRUE )
			Message( "カレントオブジェクトには面がないので、パーツ登録を中止しました" );
		Select( sel2 );
		PolyInvisible( invisible_flag );
		Select( sel1 );
	}
	obj = ObjAppend( "_temp", model_dir+"_temp.suf" );
	while( PolyLoad() )
	{
		PolyObj( obj );
		PolyAppend( FALSE );
	}
	SelectObj( obj, TRUE, SELECT_UPDATE );
	ConvexConvert();
	if( double_flag == TRUE )
	{
		j = SelectPolygons();
		PolyLoadInit();
		while( PolyLoad())
		{
			PolyInvVertex();
			PolyAppend( FALSE );
		}
		SelectObj( obj, TRUE, SELECT_XOR );
		PolyShadeInv();
		SelectObj( obj, TRUE, SELECT_UPDATE );
	}
	WriteFile( obj );
	PolyDelete();
	ObjDelete( obj );
	WriteFrame( pers_mat, model_dir+"_temp.frm", "_temp", vector(0.45,0.29,0.61));
	flag = FILE_OVERWRITE;
	for (i = 0; i < Attrs(); i++) {
		if (AttrPolygons(i) > 0) {
			AttrWriteFile(model_dir+"_temp.atr", i, flag);
			flag = FILE_APPEND;
		}
	}
	delete( model_dir+"_temp.ind" );
	fprint( model_dir+"_temp.ind", "-c128,96 -g\n"
		+"-o"+model_dir+"_temp.bmp\n"
		+model_dir+"_temp.frm\n"
		+model_dir+"_temp.atr\n"
		+model_dir+"_temp.suf" );
	Child( "rendwin @"+model_dir+"_temp.ind", TRUE );

	temp2 = ele[0];
	if( access( model_dir+"error.log" ))
		delete( model_dir+"error.log" );
	if( filecheck_flag )
	{
		temp = fullpath( ObjFile( ObjCurrent()));
		Bunkai( temp, ele );
		Child( "partsins.bat "+model_dir+" "+model_dir+"_temp.suf " + model_dir+"_temp001.bmp " + temp2 + " " + ele[1], TRUE );
	}
	else
		Child( "partsins.bat "+model_dir+" "+model_dir+"_temp.suf " + model_dir+"_temp001.bmp " + temp2, TRUE );
	if( access( model_dir+"error.log" ))
		Message( "パーツデータの登録に失敗しました" );
	else
	{
		ObjEdit( ObjCurrent(), FALSE );
		UpdateObject();
	}
	Select( sel2 );
	PolyInvisible( invisible_flag );
	Select( sel1 );
	Update( OVERWRITE );
}
